
package gblend.formatcovert;

/**
 *
 * @author cjjin
 */

/*
* Copyright 2010, Center for Advanced Information Systems, Nanyang Technological University
*
* File name: CtreeformatSort.java
*
* Abstract: Sort the Ctree format dataset by graph size
*
* Current Version:      0.1
* Auther:               Jin Changjiu
* Modified Date:        June.3,2009
*
*/

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import gblend.adjlistgraph.ALGraph;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Collections;
import java.util.Vector;
import gblend.frequentindex.Vertex;

public class CtreeformatSort {

    private static Vector<ALGraph> graphlist = new Vector<ALGraph>();
    private static ALGraph tmpgraph = null;
    private static Vector<Integer> h = new Vector<Integer>();
    private static String filename = null;

    public static void main (String[] args) throws FileNotFoundException
    {
         filename = "0.01Syn120k.fp";
         formatDotFile(filename);
    }


    public static void formatDotFile(String filename)
    {
   
	String output1=filename+".q";
        String strLine = null;
	FileOutputStream outStream1,outStream2;
	PrintStream p;
        PrintStream q;
	FileInputStream inStream;
	DataInputStream in;
	BufferedReader br;

	File fin=new File(filename);
	File fout1=new File(output1);
     
	if(fin.exists())
	{

         try {
        	outStream1=new FileOutputStream(fout1,false);
                inStream=new FileInputStream(fin);
		// Connect print stream to the output stream
                p = new PrintStream( outStream1 );
               // Get the object of DataInputStream
                in = new DataInputStream(inStream);
                br = new BufferedReader(new InputStreamReader(in));
                //Read file line by line
    		try {
                      int vn = 0;
                      int en = 0;
                      String freq =null;
                      Vector<String> nodelable = null;
                      Vector<String> edge = null;

                      while ((strLine = br.readLine()) != null)
                      {
                       // Print the content on the console
                          if(strLine.contains("t #"))
                          {
                               String[] str = strLine.split("\\s");
                              freq = str[4];
                               vn = 0;
                               en = 0;
                               nodelable = new Vector<String>();
                               edge = new Vector<String>();
                           }
                           else if(strLine.contains("v"))
                           {
                               vn++;
                               String[] strnode = strLine.split("\\s");
                               nodelable.addElement(strnode[2]);
                              
                           }
                           else if(strLine.contains("e"))
                           {
                               en++;
                               String[] stredge = strLine.split("\\s");
                               edge.addElement(stredge[1]+" "+stredge[2]);
                               
                           }
                           else
                           {
                                tmpgraph = new ALGraph(vn);
                                tmpgraph.setNnum(vn);
                                tmpgraph.setEnum(en);
                                for(int i = 0; i< nodelable.size();i++)
                                {
                                    Vertex node = new Vertex();
                                    node.setLabel(nodelable.elementAt(i));
                                    tmpgraph.addnode(node);
                                }

                                for(int i = 0; i< edge.size();i++)
                                {
                                    String[] strnode = edge.elementAt(i).split("\\s");
                                    int e1 = Integer.parseInt(strnode[0]);
                                    int e2 = Integer.parseInt(strnode[1]);
                                    tmpgraph.addEdge(e1, e2);
                                }

                                if(vn<=9)
                                    graphlist.addElement(tmpgraph);
                           }
                     }

                  GraphCompare mc = new GraphCompare();
                  Collections.sort(graphlist, mc);

                  for(int i = 0;i< graphlist.size();i++)
                  {
                   ALGraph tmpg = graphlist.elementAt(i);

                    p.println("#"+i);
                    p.println(tmpg.getNnum());
                    for(int j = 0; j< tmpg.getNnum();j++)
                    {
                        p.println(tmpg.getNode(j).getLabel());
                    }
                    p.println(tmpg.getEnum());
                    for(int m = 0; m< tmpg.getNnum();m++)
                    {
                      for(int n = 0; n< tmpg.getNnum();n++)
                      {
                        if(tmpg.getEdge(m, n)==1)
                             p.println(m+" "+n);
                      }
                    }
                    p.println();
                  }
                 //Close the input stream
                 in.close();
                 p.close();
             
		}
		catch (IOException e)
		{
		// TODO Auto-generated catch block
			e.printStackTrace();
		}
                } catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
                    e.printStackTrace();
		}
	}

    }


}
